﻿using System.Collections.Generic;
using System.Linq;
using GP.Scripts.City;
using Gp.Scripts.Core;

namespace Gp.Scripts.Core {
    /// <summary>
    /// 场景中的动态物体持久化对象
    /// </summary>
    public class EntityModel : PersistentModel<SerializedSceneObjectModel> {

        protected override void OnInit()
        {
            // Entities = new Dictionary<EntityType, Dictionary<string, BaseEntity>>();
        }


        private SerializedSceneObjectModel _saveData = new();

        public override ISerializedModel OnSave() {
            // foreach (var o in DynamicObjects) {
            //     if (o is TransDynamicObject obj) {
            //         _saveData.posInfos.RemoveAll(x => x.key == o.SaveKey);
            //         _saveData.posInfos.Add(new DynamicObjectInfo(obj, o.SaveKey));
            //     }
            //
            //     if (o is not BasePuzzle pz) continue;
            //     if (_saveData.puzzleKeys.Contains(o.SaveKey)) continue;
            //     if (pz.TriggerFlag) _saveData.puzzleKeys.Add(o.SaveKey);
            // }
            //
            // foreach (var ch in EntitySystem.Characters) {
            //     _saveData.posInfos.RemoveAll(x => x.key == ch.GetUniqueName());
            //     _saveData.posInfos.Add(new DynamicObjectInfo(ch.gameObject, ch.GetUniqueName()));
            // }
            //
            // foreach (var destroyed in DestroyedObjects.Where(destroyed =>
            //              !_saveData.destroyedItems.Contains(destroyed))) {
            //     _saveData.destroyedItems.Add(destroyed);
            // }
            //
            // DestroyedObjects.Clear();
            // var trans = ObjectHelper.PlayerCharacter.transform;
            // _saveData.playerPos = new TransformInfo(trans.position, trans.rotation);

            return _saveData;
        }


        protected override void OnLoad(SerializedSceneObjectModel data) {
            _saveData = data;
        }


        public DynamicObjectInfo GetInfoData(string saveKey) {
            return _saveData.posInfos.FirstOrDefault(x => x.key == saveKey);
        }


        public bool TriggeredFlag(string key) {
            return _saveData.puzzleKeys.Contains(key);
        }

        public TransformInfo PlayerPos() {
            return _saveData.playerPos;
        }


        public bool IsObjectDestroyed(string saveKey) {
            return _saveData.destroyedItems.Contains(saveKey);
        }
    }
}